package mysinglelist;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2024-07-11
 * Time: 10:37
 */
public class MySingLeList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(34);
        ListNode node3 = new ListNode(45);
        ListNode node4 = new ListNode(56);
        ListNode node5 = new ListNode(67);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    public void display(){
        ListNode tmp = this.head;
        while(!(tmp == null)){
            System.out.print(tmp.val+" ");
            tmp = tmp.next;
        }
    }

    public int size(){
        ListNode tmp = this.head;
        int count = 0;
        while(!(tmp == null)){
            count++;
            tmp = tmp.next;
        }
        return count;
    }

    public boolean contains(int findData){
        ListNode tmp = this.head;
        while(!(tmp == null)){
            if(tmp.val == findData){
                return true;
            }
        }
        return false;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode tmp = this.head;
        if(tmp == null){
            this.head = node;
            return;
        }
        while(!(tmp.next == null)){
            tmp = tmp.next;
        }
        tmp.next = node;
    }

    public void addIndex(int pos,int data){
        ListNode node = new ListNode(data);
        ListNode tmp = this.head;
        if(pos == 0){
            node.next = tmp;
            this.head = node;
            return;
        } else if (pos == this.size()) {
            this.addLast(data);
        } else if (pos > this.size()) {
            System.out.println("输入的位置错误！");
        }else{
            for (int i = 0; i < pos -1; i++) {
                tmp = tmp.next;
            }
            node.next = tmp.next;
            tmp.next = node;
        }
    }

    public void remove(int data){
        int flag = 1;
        ListNode tmp = this.head;
        if(this.head.val == data){
            this.head = this.head.next;
            return;
        }
        while(tmp.next != null){
            if(tmp.next.val== data){
                flag = 0;
                break;
            }
            tmp = tmp.next;
        }
        if(flag == 0){
            tmp.next = tmp.next.next;
        }else{
            System.out.println("链表中没有该元素！");
        }
    }
    public void removeAll(int data){
        ListNode prv = this.head;
        ListNode tmp = this.head.next;
        while(tmp != null){
            if(tmp.val == data){
                prv.next = tmp.next;
                tmp = tmp.next;
            }else{
                prv = tmp;
                tmp = tmp.next;
            }
        }
        if(this.head.val == data){
            this.head = this.head.next;
        }
    }

    public void clear(){
        this.head = null;
    }
}
